/**
 * 
 */
package com.topu.db.model;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author popofish
 * 
 */
public class DBTable extends ConcurrentHashMap<String, IDBColumn> implements
		IDBTable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1625063006429901047L;

	private String bindClass;
	private String description;
	private String name;
	private String parent;
	private IDBColumn primaryKey;
	private String engine;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#getBindClass()
	 */
	@Override
	public String getBindClass() {
		return this.bindClass;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#getBindClassSimplName()
	 */
	@Override
	public String getBindClassSimplName() {
		int idx = bindClass.lastIndexOf(".") + 1;
		return bindClass.substring(idx, bindClass.length());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#getDescription()
	 */
	@Override
	public String getDescription() {
		return this.description;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#getEngine()
	 */
	@Override
	public String getEngine() {
		return engine;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#getName()
	 */
	@Override
	public String getName() {
		return this.name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#getParent()
	 */
	@Override
	public String getParent() {
		return this.parent;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#getPrimaryKey()
	 */
	@Override
	public IDBColumn getPrimaryKey() {
		return this.primaryKey;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#setBindClass(java.lang.String)
	 */
	@Override
	public void setBindClass(String bindClass) {
		this.bindClass = bindClass;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#setDescription(java.lang.String)
	 */
	@Override
	public void setDescription(String des) {
		this.description = des;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#setEngine(java.lang.String)
	 */
	@Override
	public void setEngine(String engine) {
		this.engine = engine;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#setName(java.lang.String)
	 */
	@Override
	public void setName(String name) {
		this.name = name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBTable#setParent(java.lang.String)
	 */
	@Override
	public void setParent(String parent) {
		this.parent = parent;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.topu.db.model.IDBTable#setPrimaryKey(com.topu.db.model.IDBColumn)
	 */
	@Override
	public void setPrimaryKey(IDBColumn column) {
		this.primaryKey = column;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.ISqlWrapper#wrapSQL()
	 */
	@Override
	public String wrapSQL() {
		String SQL = "CREATE TABLE `" + name + "` (";
		for (IDBColumn column : values()) {
			SQL += "\n\t" + column.wrapSQL() + ",";
		}
		SQL += "\n\tPRIMARY KEY (";
		if (getPrimaryKey() != null) {
			SQL += "`" + primaryKey.getColumn() + "`";
		}
		SQL += ")";

		for (IDBColumn column : values()) {
			if (column.isUnique()) {
				SQL += ",\n\tUNIQUE INDEX IDX_" + column.getColumn() + "("
						+ column.getColumn() + ")";
			} else if (column.isIndex()) {
				SQL += ",\n\tINDEX IDX_" + column.getColumn() + "("
						+ column.getColumn() + ")";
			}
		}

		SQL += "\n) ENGINE=" + engine
				+ " DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;";

		return SQL;
	}
}
